﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public class AxleCacheManager
    {
        public Axle this[string aName]
        {
            get
            {
                if (!string.IsNullOrEmpty(aName) && s_axleList != null)
                {
                    if (s_axleList.ContainsKey(aName))
                    {
                        return s_axleList[aName];
                    }
                }
                return null;
            }
        }

        private static Dictionary<string, Axle> s_axleList = new Dictionary<string, Axle>();

        public static Dictionary<string, Axle> AxleList
        {
            get { return AxleCacheManager.s_axleList; }
        }
        private static Dictionary<string, Axle> s_axleByIdList = new Dictionary<string, Axle>();
        private static int s_axleId = 1;
        public static void AddAxle(Axle axle)
        {
            if (!AxleList.ContainsKey(axle.Name))
            {
                axle.ID = s_axleId;
                AxleList.Add(axle.Name, axle);
                s_axleByIdList.Add(s_axleId.ToString(), axle);
                s_axleId++;
            }
        }

        public static Axle getAxle(string name)
        {
            if (AxleList.ContainsKey(name))
            {
                return AxleList[name];
            }
            return null;
        }

        public static Axle getAxle(int id)
        {
            return getAxleById(id.ToString());
        }

        public static Axle getAxleById(string id)
        {
            if (s_axleByIdList.ContainsKey(id))
            {
                return s_axleByIdList[id];
            }
            return null;
        }

        static DB.BLL.Axle m_aManager = new DB.BLL.Axle();

        public static void Update(string axleName)
        {
            if (AxleList.ContainsKey(axleName))
            {
                if (!m_aManager.Exists(AxleList[axleName].ID))
                {
                    m_aManager.Add(AxleList[axleName].DBModel);
                }
                else
                {
                    m_aManager.Update(AxleList[axleName].DBModel);
                }
            }
        }

        public static void Update(Axle axle)
        {
            AxleList[axle.Name] = axle;
            s_axleByIdList[axle.ID.ToString()] = axle;

            if (!m_aManager.Exists(axle.ID))
            {
                m_aManager.Add(axle.DBModel);
            }
            else
            {
                m_aManager.Update(axle.DBModel);
            }
        }

        public static void InitFromDB()
        {
            DB.BLL.Axle axManager = new DB.BLL.Axle();
            List<DB.Model.Axle> axList = null;
            try
            {
                axList = axManager.GetModelList(string.Empty);
            }
            catch (Exception ex)
            {
                axList = new List<DB.Model.Axle>();
            }
            if (axList != null && axList.Count > 0)
            {
                foreach (DB.Model.Axle aModel in axList)
                {
                    Axle ax = new Axle(aModel);
                    AddAxle(ax);
                }
            }
            else
            {

                List<KZ400B.Axle> m_axleList = new List<KZ400B.Axle>();
                
                if (m_axleList.Count <= 0)
                {
                    m_axleList.Add(new KZ400B.Axle("X"));
                    m_axleList.Add(new KZ400B.Axle("Y"));
                    m_axleList.Add(new KZ400B.Axle("Z"));
                    m_axleList.Add(new KZ400B.Axle("T"));
                    m_axleList.Add(new KZ400B.Axle("U"));
                    m_axleList.Add(new KZ400B.Axle("W"));
                }
                foreach (KZ400B.Axle axle in m_axleList)
                {
                    AddAxle(axle);
                }
            }
        }
    }
}
